<?php


namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\model\CompanyModel;
use think\cache\driver\Redis;
use think\Db;
use think\facade\Cache;
use app\common\model\ApplicationsModel;
use app\common\service\ExcelService;
use think\facade\Env;


class Equipment extends AdminController
{
    protected $applicationsModel;


    // 初始化构造方法
    public function __construct()
    {
        parent::__construct();
        $this->applicationsModel = new ApplicationsModel(); // 账户表模型

    }

    /**
     * 设备列表
     */
    public function index()
    {
        $app_sn = input('param.app_sn') ?? 'cloud2019';
        $basic_data = [
            'app_sn' => $app_sn
        ];
        return $this->fetch('', $basic_data);
    }

    /*
     * 设备库列表
     *
     * */
    public function libary()
    {
        $app_sn = input('param.app_sn') ?? 'cloud2019';
        $api_host = $this->applicationsModel->getApiHost($app_sn);
        $basic_data = [
            'app_sn' => $app_sn,
            'api_host' => $api_host
        ];
        return $this->fetch('', $basic_data);
    }

    /*
     * 添加设备库
     * */
    public function add()
    {
        $app_sn = input('param.app_sn') ?? 'cloud2019';
        $api_host = $this->applicationsModel->getApiHost($app_sn);
        //渲染页面
        if (!$this->request->isPost()) {
            //获取主板列表
            $post['page'] = 1;
            $post['pageSize'] = 999;
            $api_url = $api_host . 'Boss/Mainboard/getLists';
            $result = http_post($api_url, json_encode($post));

            $this->assign('mainboard', !empty($result) ? $result['data'] : []);
            return $this->fetch('', ['app_sn' => $app_sn]);
        } else {
            // 接收参数
            $post = $this->request->post();
            if (empty($post['device_no'])) {
                return json(['code' => 0, 'msg' => '请输入设备编号']);
            }
            if (empty($post['model_id'])) {
                return json(['code' => 0, 'msg' => '请选择主板型号']);
            }

            $api_url = $api_host . 'Boss/Equipment/add';
            $result = http_post($api_url, json_encode($post));

            if ($result['code'] == 1) {
                return json(['code' => 1, 'msg' => '操作成功']);
            }
            return json(['code' => 0, 'msg' => '操作失败']);

        }


    }

    /*
     *
     * 主板列表
     * */
    public function mainboard()
    {
        $app_sn = input('param.app_sn') ?? 'cloud2019';
        $api_host = $this->applicationsModel->getApiHost($app_sn);
        $basic_data = [
            'app_sn' => $app_sn,
            'api_host' => $api_host
        ];
        return $this->fetch('', $basic_data);
    }


    /*
     * 添加主板型号
     *
     * */
    public function add_mainboard()
    {
        $app_sn = input('param.app_sn') ?? 'cloud2019';
        $api_host = $this->applicationsModel->getApiHost($app_sn);
        // 渲染添加页面
        if (!$this->request->isPost()) {
            $basic_data = [
                'app_sn' => $app_sn,
                'api_host' => $api_host
            ];
            return $this->fetch('', $basic_data);
        } // 处理添加操作
        else {
            // 接收参数
            $post = $this->request->post();
            if (empty($post['model'])) {
                return json(['code' => 0, 'msg' => '请输入主板型号']);
            }
            if (empty($post['edition'])) {
                return json(['code' => 0, 'msg' => '请输入版本']);
            }
            if (empty($post['num'])) {
                return json(['code' => 0, 'msg' => '请输入数量']);
            }

            $api_url = $api_host . 'Boss/Mainboard/add';
            $result = http_post($api_url, json_encode($post));

            if ($result['code'] == 1) {
                return json(['code' => 1, 'msg' => '操作成功']);
            }
            return json(['code' => 0, 'msg' => '操作失败']);
        }

    }

    /*
     * 编辑主板
     *
     * */
    public function edit_mainboard()
    {
        $app_sn = input('param.app_sn') ?? 'cloud2019';
        $api_host = $this->applicationsModel->getApiHost($app_sn);
        if (!$this->request->isPost()) {
            //接收参数
            $mainboard_id = $id = $this->request->get('mainboard_id');

            $post['mainboard_id'] = $mainboard_id;
            $api_url = $api_host . 'Boss/Mainboard/getInfo';
            $result = http_post($api_url, json_encode($post));

            $basic_data = [
                'app_sn' => $app_sn,
                'api_host' => $api_host,
                'data'=>!empty($result['data'])?$result['data']:[]
            ];
            return $this->fetch('add_mainboard', $basic_data);
        }else{
            // 接收参数
            $post = $this->request->post();
            if (empty($post['mainboard_id'])) {
                return json(['code' => 0, 'msg' => '缺少参数']);
            }
            if (empty($post['model'])) {
                return json(['code' => 0, 'msg' => '请输入主板型号']);
            }
            if (empty($post['edition'])) {
                return json(['code' => 0, 'msg' => '请输入版本']);
            }
            if (empty($post['num'])) {
                return json(['code' => 0, 'msg' => '请输入数量']);
            }

            $api_url = $api_host . 'Boss/Mainboard/edit';
            $result = http_post($api_url, json_encode($post));

            if ($result['code'] == 1) {
                return json(['code' => 1, 'msg' => '操作成功']);
            }
            return json(['code' => 0, 'msg' => '操作失败']);


        }


    }

    /*
     *
     *二维码列表
     */
    public function qrcode()
    {
        $app_sn = input('param.app_sn') ?? 'cloud2019';
        $api_host = $this->applicationsModel->getApiHost($app_sn);
        $basic_data = [
            'app_sn' => $app_sn,
            'api_host' => $api_host
        ];
        return $this->fetch('', $basic_data);

    }

    /*
     * 生成设备二维码
     *
     */
    public function add_qrcode()
    {
        $app_sn = input('param.app_sn') ?? 'cloud2019';
        $api_host = $this->applicationsModel->getApiHost($app_sn);
        if (!$this->request->isPost()) {
            //查询企业信息
            $company_model = new CompanyModel();
            $company_list = $company_model->getAll(['app_sn'=>$app_sn], 'company_id,short_name_en,company_name');

            $this->assign('company_list', $company_list);
            $basic_data = [
                'app_sn' => $app_sn,
                'api_host' => $api_host
            ];
            return $this->fetch('', $basic_data);
        } else {
            // 接收参数
            $post = $this->request->post();
            if (empty($post['qrcode_no'])) {
                return json(['code' => 0, 'msg' => '二维码编号格式错误']);
            }
            if (!in_array(strlen($post['qrcode_no']), [2, 8])) {
                return json(['code' => 0, 'msg' => '二维码编号长度错误']);
            }
            if ($post['type'] == 2 && strlen($post['qrcode_no']) != 2) {
                return json(['code' => 0, 'msg' => '批量生成请输入2位数字前缀']);
            }

            $api_url = $api_host. 'Boss/Qrcode/add';
            $result = http_post($api_url, json_encode($post));

            if ($result['code'] == 1) {
                return json(['code' => 1, 'msg' => '操作成功']);
            }
            return json(['code' => 0, 'msg' => '操作失败']);


        }


    }

    /*
     * 下载
     *
     */
    public function download()
    {
        $param = input('param.');


        $this->assign('qrcode_no', $param['qrcode_no']);
        return $this->fetch();
    }

    /*
     *
     * 导出excel
     * */
    public function export()
    {
        $app_sn = input('param.app_sn') ?? 'cloud2019';
        $api_host = $this->applicationsModel->getApiHost($app_sn);
        $param = input('param.');
        $api_url = $api_host. 'Boss/Qrcode/exportQrcode';
        $result = http_post($api_url, json_encode($param));
        $url = $api_url. $result['data']['url'];
        header('location:' . $url);
        if ($result['code'] == 1) {
            return json(['code' => 1, 'msg' => '操作成功']);
        }
        return json(['code' => 0, 'msg' => '操作失败']);

    }

    /**
     * 导入主板库
     */
    public function import()
    {
        $app_sn = input('param.app_sn') ?? 'cloud2019';
        $api_host = $this->applicationsModel->getApiHost($app_sn);

        if (!$this->request->isPost()) {
            //获取主板列表
            $post['page'] = 1;
            $post['pageSize'] = 999;
            $api_url = $api_host . 'Boss/Mainboard/getLists';
            $result = http_post($api_url, json_encode($post));

            $basic_data = [
                'app_sn' => $app_sn,
                'api_host' => $api_host,
                'mainboard'=>!empty($result) ? $result['data'] : []
            ];
            return $this->fetch('', $basic_data);

        }else{
            $param = input('param.');

            if (empty($param['model_id'])) {
                returnJson(0, '请选择主板型号');
            }
            if (empty($param['file_url'])) {
                returnJson(0, '请上传文件');
            }
            $file_url = UploadImgPath($param['file_url']);

            file_put_contents($param['file_url'],file_get_contents($file_url));
            $array = ExcelService::readExcel(Env::get('root_path').'public/'.$param['file_url']);
            unset($array[1]);

            $data=[
                'model_id'=>$param['model_id'],
                'arr'=>json_encode($array)
            ];
            $api_url = $api_host. 'Boss/Equipment/imports';
            $result = http_post($api_url, json_encode($data));

            if ($result['code'] == 1) {
                return json(['code' => 1, 'msg' => '操作成功']);
            }
            return json(['code' => 0, 'msg' => '操作失败']);

        }

    }


}